package cn.com.api.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiT_member_wemedia;
import cn.com.api.entity.ApiU_favorite;
import cn.com.api.entity.ApiU_follow;
import cn.com.api.entity.U_article;
import cn.com.api.entity.U_article_cate;
import cn.com.api.entity.U_article_resource;
import cn.com.api.entity.U_cate;
import cn.com.api.entity.U_label;
import cn.com.api.entity.Ylj_Article_Label_Obj;
import cn.com.api.entity.Ylj_Article_Obj;
import cn.com.api.entity.Ylj_Index_article_Obj;
import cn.com.api.mapper.ApiT_member_wemediaMapper;
import cn.com.api.mapper.ApiU_article_replyMapper;
import cn.com.api.mapper.ApiU_favoriteMapper;
import cn.com.api.mapper.ApiU_followMapper;
import cn.com.api.mapper.ApiU_zanMapper;
import cn.com.api.mapper.U_articleMapper;
import cn.com.api.mapper.U_article_cateMapper;
import cn.com.api.mapper.U_article_resourceMapper;
import cn.com.api.mapper.U_cateMapper;
import cn.com.api.mapper.U_labelMapper;
import cn.com.api.mapper.base.U_article_replyMapperBase;
import cn.com.api.service.U_articleService;
import cn.com.api.service.impl.base.U_articleServiceImplBase;
import cn.com.util.DateUtil;
import cn.com.util.HourGap;
import cn.com.util.PageInfo;
import cn.com.util.Util;

@Service
public class U_articleServiceImpl extends U_articleServiceImplBase implements U_articleService {
	@Autowired
	private U_articleMapper u_articleMapper;
	@Autowired
	private ApiU_article_replyMapper apiU_article_replyMapper;
	@Autowired
	private U_article_cateMapper U_article_cateMapper;
	@Autowired
	private U_cateMapper u_cateMapper;
	@Autowired
	private U_labelMapper u_labelMapper;
	@Autowired
	private ApiU_zanMapper u_zanMapper;
	@Autowired
	private ApiU_favoriteMapper apiU_favoriteMapping;
	@Autowired
	private ApiU_followMapper apiU_followMapper;
	@Autowired
	private ApiT_member_wemediaMapper apiT_member_wemediaMapper;
	@Autowired
	private U_article_resourceMapper u_article_resourceMapper;

	public List<Ylj_Index_article_Obj> findU_articlesListPage(int exclideSize, int pageSize) {
		return this.u_articleMapper.findU_articlesListPage(exclideSize, pageSize);
	}

	public List<Ylj_Article_Label_Obj> findLabel(int rootCateID, int rootCateType) {
		List<Ylj_Article_Label_Obj> result = new ArrayList<Ylj_Article_Label_Obj>();

		List<Map<String, Object>> list = this.u_articleMapper.findLevel(rootCateID, rootCateType);
		if (list.size() == 0) {
			return result;
		}
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = (Map) list.get(i);
			int cart_id = ((Integer) map.get("id")).intValue();
			String cate_name = (String) map.get("cate_name");
			Ylj_Article_Label_Obj obj = new Ylj_Article_Label_Obj();
			obj.setCart_id(Integer.valueOf(cart_id));
			obj.setCate_name(cate_name);

			List<Map<String, Object>> list2 = this.u_articleMapper.findLevel(cart_id, rootCateType);

			List<Ylj_Article_Label_Obj> child = new ArrayList<Ylj_Article_Label_Obj>();
			if (list2.size() == 0) {
				obj.setChild(child);
			} else {
				for (int j = 0; j < list2.size(); j++) {
					Map<String, Object> map2 = (Map) list2.get(j);
					int cart_id2 = ((Integer) map2.get("id")).intValue();
					String cate_name2 = (String) map2.get("cate_name");
					Ylj_Article_Label_Obj obj2 = new Ylj_Article_Label_Obj();
					obj2.setCart_id(Integer.valueOf(cart_id2));
					obj2.setCate_name(cate_name2);
					obj2.setChild(new ArrayList<Ylj_Article_Label_Obj>());
					child.add(obj2);
				}
				obj.setChild(child);
			}
			result.add(obj);
		}
		return result;
	}

	public List<Ylj_Article_Label_Obj> findAllLabel() {
		List<Ylj_Article_Label_Obj> result = new ArrayList<Ylj_Article_Label_Obj>();

		List<Map<String, Object>> list = this.u_articleMapper.findAllLevel(1);
		if (list.size() == 0) {
			return result;
		}
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = (Map) list.get(i);
			int cart_id = ((Integer) map.get("id")).intValue();
			String cate_name = (String) map.get("cate_name");
			Ylj_Article_Label_Obj obj = new Ylj_Article_Label_Obj();
			obj.setCart_id(Integer.valueOf(cart_id));
			obj.setCate_name(cate_name);

			List<Map<String, Object>> list2 = this.u_articleMapper.findAllLevel(cart_id);

			List<Ylj_Article_Label_Obj> child = new ArrayList<Ylj_Article_Label_Obj>();
			if (list2.size() == 0) {
				obj.setChild(child);
			} else {
				for (int j = 0; j < list2.size(); j++) {
					Map<String, Object> map2 = (Map) list2.get(j);
					int cart_id2 = ((Integer) map2.get("id")).intValue();
					String cate_name2 = (String) map2.get("cate_name");
					Ylj_Article_Label_Obj obj2 = new Ylj_Article_Label_Obj();
					obj2.setCart_id(Integer.valueOf(cart_id2));
					obj2.setCate_name(cate_name2);

					List<Map<String, Object>> list3 = this.u_articleMapper.findAllLevel(cart_id2);

					List<Ylj_Article_Label_Obj> child2 = new ArrayList<Ylj_Article_Label_Obj>();
					if (list3.size() == 0) {
						obj2.setChild(child2);
					} else {
						for (int k = 0; k < list3.size(); k++) {
							Map<String, Object> map3 = (Map) list3.get(k);
							int cart_id3 = ((Integer) map3.get("id")).intValue();
							String cate_name3 = (String) map3.get("cate_name");
							Ylj_Article_Label_Obj obj3 = new Ylj_Article_Label_Obj();
							obj3.setCart_id(Integer.valueOf(cart_id3));
							obj3.setCate_name(cate_name3);

							obj3.setChild(new ArrayList<Ylj_Article_Label_Obj>());
							child2.add(obj3);
						}
						obj2.setChild(child2);
					}
					child.add(obj2);
				}
				obj.setChild(child);
			}
			result.add(obj);
		}
		return result;
	}

	// 文章總數
	public int queryArticleTotal(String user_id, String keyword, String cate_id) {
		String query = "";
		if (!keyword.equals("")) {
			String[] s = keyword.split(";");
			for (int i = 0; i < s.length; i++) {
				if (i == 0) {
					query = query + " and ( a.title like '%" + s[0] + "%' or a.label like '%" + s[i] + "%' ";
				} else {
					query = query + " or  a.title like '%" + s[i] + "%' or a.label like '%" + s[i] + "%' ";
				}
			}
			if (s.length != 0) {
				query = query + ") ";
			}
		}
		if (!cate_id.equals("")) {
			query = query + " and a.id in( select c.article_id from u_cate b , u_article_cate c where b.parent_id="
					+ cate_id + " and c.cate_id=b.id ) ";
		} else {
			// 29 是u_cate表的育儿知识的id（整个app都是在育儿知识分类下的文章）
			query = query + " and a.id in( select c.article_id from u_cate b , u_article_cate c where b.parent_id=" + 29
					+ " and c.cate_id=b.id ) ";
		}
		int result = u_articleMapper.queryArticleTotal(query);

		return result;
	}

	public List<Ylj_Article_Obj> queryArticleListPage(String user_id, String keyword, String cate_id,
			String rootCateType, int exclideSize, int pageSize) {
		System.out.println("==============================<<<<queryArticleListPage>>>>===========================");
		String query = "";
		if (!keyword.equals("")) {
			String[] s = keyword.split(" ");
			for (int i = 0; i < s.length; i++) {
				if (i == 0) {
					query = query + " and ( a.title like '%" + s[0] + "%'  or a.label like '%" + s[0]
							+ "%' or c.cate_name like '%" + s[0] + "%' ";
				} else {
					query = query + " or  a.title like '%" + s[i] + "%' or a.label like '%" + s[i]
							+ "%' or c.cate_name like '%" + s[i] + "%' ";
				}
			}
			if (s.length != 0) {
				query = query + ") ";
			}
		}
		// if (!cate_id.equals("")) {
		// query = query +
		// " and a.id in( select c.article_id from u_cate b , u_article_cate c
		// where ( b.parent_id="
		// + cate_id + " or b.id= "+cate_id + " ) and c.cate_id=b.id ) ";
		// }else{
		// // 29 是u_cate表的育儿知识的id（整个app都是在育儿知识分类下的文章）
		// query = query +
		// " and a.id in( select c.article_id from u_cate b , u_article_cate c
		// where b.parent_id="
		// + 29 + " and c.cate_id=b.id ) ";
		// }
		// 指定分类查找
		if (!cate_id.equals("")) {
			query = query + " and (c.parent_id =" + cate_id + " or c.id = " + cate_id + ")";
		} else {
			query = query + " and c.parent_id = 29 ";
		}

		/*
		 * System.out.println("keyword : " + keyword); if (!keyword.equals(""))
		 * { U_search_his sHis = new U_search_his();
		 * sHis.setSearch_content(keyword);
		 * sHis.setSearch_time(HourGap.getNowTime());
		 * sHis.setType(Integer.valueOf(rootCateType)); int ret =
		 * this.u_search_hisMapper.insertU_search_his(sHis); }
		 */
		System.out.println("query : " + query);
		List<Ylj_Article_Obj> result = this.u_articleMapper.queryArticleListPage(rootCateType, query, exclideSize,
				pageSize);
		for (int i = 0; i < result.size(); i++) {
			String article_id = ((Ylj_Article_Obj) result.get(i)).getArticle_id();
			System.out.println("article_id<" + i + ">: " + article_id);
			((Ylj_Article_Obj) result.get(i))
					.setAdd_time(HourGap.intervalTime(((Ylj_Article_Obj) result.get(i)).getAdd_time()));

			List<String> result1 = new ArrayList<String>();

			List<Map<String, Object>> list = u_articleMapper.findCateNames(Integer.parseInt(article_id));

			for (int j = 0; j < list.size(); j++) {
				Map<String, Object> map = (Map) list.get(j);
				String cate_name = (String) map.get("cate_name");
				result1.add(cate_name);
			}
			((Ylj_Article_Obj) result.get(i)).setCate_names(result1);

			List<Integer> result2 = new ArrayList<Integer>();
			List<Map<String, Object>> list2 = u_articleMapper.findCateIds(Integer.parseInt(article_id));
			for (int j = 0; j < list2.size(); j++) {
				Map<String, Object> map = (Map) list2.get(j);
				int id = (Integer) map.get("id");
				result2.add(id);
			}

			if (result2.size() > 1 && result2.get(0) > result2.get(1)) {
				((Ylj_Article_Obj) result.get(i)).setCate_id(String.valueOf(result2.get(0)));
			} else if (result2.size() > 1) {
				((Ylj_Article_Obj) result.get(i)).setCate_id(String.valueOf(result2.get(1)));
			} else {
				((Ylj_Article_Obj) result.get(i)).setCate_id(String.valueOf(result2.get(0)));
			}

		}
		return result;
	}

	// 首页获取文章列表
	public List<Ylj_Article_Obj> queryIndexArticleListPage(String user_id, String rootCateType, int pageSize) {
		System.out
				.println("==============================<<<<queryIndexArticleListPage>>>>===========================");
		List<Ylj_Article_Obj> result = this.u_articleMapper.queryIndexArticleListPage(rootCateType, pageSize);
		for (int i = 0; i < result.size(); i++) {
			String article_id = ((Ylj_Article_Obj) result.get(i)).getArticle_id();
			System.out.println("article_id<" + i + ">: " + article_id);
			((Ylj_Article_Obj) result.get(i))
					.setAdd_time(HourGap.intervalTime(((Ylj_Article_Obj) result.get(i)).getAdd_time()));
			/*
			 * if (!user_id.equals("")) { U_like u_like =
			 * this.U_likeMapper.findUserHasLike( Integer.parseInt(user_id),
			 * Integer.parseInt(article_id), 1); if (u_like != null) {
			 * ((Ylj_Article_Obj) result.get(i)).setHas_like("1"); } } int
			 * comment_count = this.U_commentMapper.findCommentCount(Integer
			 * .parseInt(article_id));
			 * 
			 * ((Ylj_Article_Obj) result.get(i)).setComment_count(Integer
			 * .valueOf(comment_count));
			 */
			List<String> result1 = new ArrayList<String>();

			List<Map<String, Object>> list = u_articleMapper.findCateNames(Integer.parseInt(article_id));

			for (int j = 0; j < list.size(); j++) {
				Map<String, Object> map = (Map) list.get(j);
				String cate_name = (String) map.get("cate_name");
				result1.add(cate_name);
			}
			((Ylj_Article_Obj) result.get(i)).setCate_names(result1);

			List<Integer> result2 = new ArrayList<Integer>();
			List<Map<String, Object>> list2 = u_articleMapper.findCateIds(Integer.parseInt(article_id));
			for (int j = 0; j < list2.size(); j++) {
				Map<String, Object> map = (Map) list2.get(j);
				int id = (Integer) map.get("id");
				result2.add(id);
			}

			if (result2.size() > 1 && result2.get(0) > result2.get(1)) {
				((Ylj_Article_Obj) result.get(i)).setCate_id(String.valueOf(result2.get(0)));
			} else if (result2.size() > 1) {
				((Ylj_Article_Obj) result.get(i)).setCate_id(String.valueOf(result2.get(1)));
			} else {
				((Ylj_Article_Obj) result.get(i)).setCate_id(String.valueOf(result2.get(0)));
			}

		}
		return result;
	}

	// 文章分類名
	public List<String> findCateNames(int article_id) {
		List<String> result = new ArrayList<String>();

		List<Map<String, Object>> list = u_articleMapper.findCateNames(article_id);

		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = (Map) list.get(i);
			String cate_name = (String) map.get("cate_name");
			result.add(cate_name);
		}

		return result;
	}

	// 添加文章返回id
	public int insertU_articleReturnId(U_article article) {

		int ret = this.u_articleMapper.U_articleInsertion(article);
		return ret;
	}

	/*
	 * ** 插入文章分类关联表 废弃 不插入父类 *
	 */
	// public void insertArticle_cate(String article_id, String cate_id) {
	//
	// String cateId = cate_id;
	// U_cate cate = u_cateMapper.findU_cateById(Integer.valueOf(cateId));
	// Integer parent_id = cate.getParent_id();
	//
	// U_article_cate ac = new U_article_cate();
	// ac.setArticle_id(Integer.valueOf(article_id));
	// ac.setCate_id(Integer.valueOf(cateId));
	// U_article_cateMapper.insertU_article_cate(ac);
	//
	// U_article_cate ac1 = new U_article_cate();
	// ac1.setArticle_id(Integer.valueOf(article_id));
	// ac1.setCate_id(parent_id);
	// // U_article_cateMapper.insertU_article_cate(ac1);
	// }

	// 插入文章分类表
	public boolean addCate(String level1, String level2, String cate_name, String cate_desc, String sort) {

		try {
			int cate_id = 0;
			if (!level1.equals("0")) {

				cate_id = Integer.parseInt(level1);
				if (!level2.equals("0")) {
					cate_id = Integer.parseInt(level2);
				}
			}

			U_cate cate = new U_cate();
			cate.setCate_desc(cate_desc);
			cate.setCate_name(cate_name);
			cate.setCate_type(0);
			// cate.setCreater_id(0);
			cate.setKeywords("");
			// cate.setModified_by(0);
			cate.setModified_time("");
			// cate.setParent_id(cate_id);
			cate.setSort(Integer.parseInt(sort));
			int ret = u_cateMapper.insertU_cate(cate);
			if (ret == 0) {
				return false;
			} else {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	// 插入文章label
	public boolean addLabel(String level1, String cate_name, String sort) {

		try {
			int cate_id = 0;
			if (!level1.equals("0")) {

				cate_id = Integer.parseInt(level1);
			}
			String now = HourGap.getNowTime();

			U_label cate = new U_label();
			cate.setCreate_time(now);
			cate.setLabel_name(cate_name);
			// cate.setParent_id(cate_id);
			cate.setSort(Integer.parseInt(sort));
			int ret = u_labelMapper.insertU_label(cate);
			if (ret == 0) {
				return false;
			} else {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	/**
	 * 查询分类下的文章(含子类)
	 */
	@Override
	public List<U_article> findU_articlesByCateId(String cate_id) {
		//
		List<String> cate_list = new ArrayList<String>();
		cate_list.add(cate_id);
		// 子类
		List<U_cate> child_cates = u_cateMapper.findU_catesByParent_id(cate_id);
		for (U_cate cate : child_cates) {
			cate_list.add(cate.getId().toString());
			// 子类的子类（三级类）
			List<U_cate> child_child_cates = u_cateMapper.findU_catesByParent_id(cate.getId().toString());
			for (U_cate child_cate : child_child_cates) {
				cate_list.add(child_cate.getId().toString());
			}
		}
		//
		List<U_article> arts = u_articleMapper.findU_articlesByCateId(cate_list);
		return arts;
	}

	@Override
	public List<U_article> findU_articlesOrderBy(Integer parent_Id, String title) {
		// ,str
		List<U_article> arts = u_articleMapper.findU_articlesOrderBy(parent_Id, title);
		return arts;
	}

	@Override
	public List<Map<String, Object>> findArticleLikeTitle(String title) {
		List<Map<String, Object>> arts = u_articleMapper.findArticleLikeTitle(title);
		return arts;
	}

	public List<Map<String, Object>> allfindU_articles() {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<U_cate> list = u_cateMapper.findU_cates();
		for (U_cate u_cate : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			List<U_article> articalelist = u_articleMapper.findU_articlesByCateid(u_cate.getId() + "");
			map.put("catename", u_cate.getCate_name());
			map.put("data", articalelist);
			result.add(map);
		}
		return result;
	}

	public List<U_article> findU_articlesByMap(Map param, PageInfo pageInfo) {

		List<U_article> article_list = new ArrayList<U_article>();
		if (param.containsKey("cate_id")) {
			String cate_id = param.get("cate_id").toString();
			String cate_name = param.get("cate_name").toString();
			U_cate cate = this.u_cateMapper.findU_cateById(Integer.valueOf(cate_id));
			if (cate.getCate_type() == 1) {// 城市
				param.put("city", cate_name);
				article_list = u_articleMapper.findU_articlesByCityListPage(param, pageInfo);
			} else {
				if (cate.getParent_id() == 1) {// 查询头条文章
					article_list = u_articleMapper.findU_articlesByMapListPage(param, pageInfo);
				} else {// 查询帮助文档
					article_list = u_articleMapper.findHelparticlesByMapListPage(param, pageInfo);
				}
			}

			for (U_article art : article_list) {
				// 时间显示转换
				try {
					String sDate = art.getAdd_time();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Date date = sdf.parse(sDate);
					art.setAdd_time(DateUtil.fromToday(date));
				} catch (Exception ex) {
					System.out.println(ex.getMessage());
					art.setAdd_time("");
				}
				// 取评论数
				Map<String, String> params = new HashMap<String, String>();
				params.put("article_id", art.getId().toString());
				Integer commentCount = this.apiU_article_replyMapper.getCommentCountByMap(params);
				art.setComment_count(commentCount);

				// 图说中图片个数， article_type=2
				if (art.getArticle_type() == 2) {
					List<U_article_resource> resouce_list = this.u_article_resourceMapper
							.getArticleResourcesByArticleID(art.getId().toString());
					art.setImg_count(resouce_list.size());
				}
			}

		}
		return article_list;
	}

	// 获取自媒体所发的头条
	public List<U_article> findU_articlesByUserID(Map map, PageInfo pageInfo) {
		List<U_article> article_list = new ArrayList<U_article>();
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("user_id", Integer.valueOf(map.get("to_uid").toString()));

		List<ApiT_member_wemedia> wemedia = this.apiT_member_wemediaMapper.findT_member_wemediasByMap(params);

		if (wemedia != null) {
			String org_id = wemedia.get(0).getId().toString();
			params.clear();
			params.put("org_id", org_id);
			if (map.containsKey("status")) {
				params.put("status", map.get("status").toString());
			}
			article_list = u_articleMapper.findWeMediaU_articlesByMapListPage(params, pageInfo);
			for (U_article art : article_list) {
				// 时间显示转换
				try {
					String sDate = art.getAdd_time();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Date date = sdf.parse(sDate);
					art.setAdd_time(DateUtil.fromToday(date));
				} catch (Exception ex) {
					System.out.println(ex.getMessage());
					art.setAdd_time("");
				}
				// 取评论数
				// Map<String, String> params = new HashMap<String, String>();
				params.clear();
				params.put("article_id", art.getId().toString());
				Integer commentCount = this.apiU_article_replyMapper.getCommentCountByMap(params);
				art.setComment_count(commentCount);
			}
		}
		return article_list;
	}

	// 查询文章
	public List<U_article> seachU_articles(String keyword, PageInfo pageInfo) {

		List<U_article> article_list = new ArrayList<U_article>();
		article_list = u_articleMapper.getArticleByWordsListPage(keyword, pageInfo);

		for (U_article art : article_list) {
			// 时间显示转换
			try {
				String sDate = art.getAdd_time();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = sdf.parse(sDate);
				art.setAdd_time(DateUtil.fromToday(date));
			} catch (Exception ex) {
				System.out.println(ex.getMessage());
				art.setAdd_time("");
			}
			// 取评论数
			Map<String, String> params = new HashMap<String, String>();
			params.put("article_id", art.getId().toString());
			Integer commentCount = this.apiU_article_replyMapper.getCommentCountByMap(params);
			art.setComment_count(commentCount);

			// 图说中图片个数， article_type=2
			if (art.getArticle_type() == 2) {
				List<U_article_resource> resouce_list = this.u_article_resourceMapper
						.getArticleResourcesByArticleID(art.getId().toString());
				art.setImg_count(resouce_list.size());
			}
		}
		return article_list;
	}

	// 根据文章ID取文章详情
	public U_article findU_articleInfoByMap(Map param) {
		Map<String, String> params = new HashMap<String, String>();
		U_article art = this.u_articleMapper.getArticleInfo(param);

		List<U_article_cate> cates = U_article_cateMapper.findU_article_catesByArticle_id(art.getId().toString());
		art.setArticle_cates(cates);
		// 评论数
		params.put("article_id", param.get("article_id").toString());
		Integer commentCount = this.apiU_article_replyMapper.getCommentCountByMap(params);
		art.setComment_count(commentCount);
		// 点赞数
		params.clear();
		params.put("conn_id", art.getId().toString());
		params.put("zan_type", "3");
		Integer zan_count = this.u_zanMapper.getZanCount(params);
		art.setZan_count(zan_count);
		// 是否点赞
		if (param.containsKey("user_id")) {
			params.put("user_id", param.get("user_id").toString());
			Integer is_zan = this.u_zanMapper.checkZan(params);
			art.setIs_zan(is_zan);
		} else {
			art.setIs_zan(0);
		}
		// 是否收藏
		if (param.containsKey("user_id")) {
			params.put("zan_type", "5");
			Integer is_favorite = this.apiU_favoriteMapping.checkFavorite(params);
			art.setIs_favorite(is_favorite);
		} else {
			art.setIs_favorite(0);
		}

		// 判断article_type == 2,图说
		if (art.getArticle_type() == 2) {
			List<U_article_resource> artResources = this.u_article_resourceMapper
					.getArticleResourcesByArticleID(art.getId().toString());
			art.setArticle_resources(artResources);
		}
		if (art.getOrg_id() != null && !"".equals(art.getOrg_id())) {
			// 获取发布机构Logo和名称
			ApiT_member_wemedia member_wemedia = this.apiT_member_wemediaMapper
					.findT_member_wemediaById(Integer.valueOf(art.getOrg_id()));
			if (!Util.isNull(member_wemedia)) {
				art.setPublish_org_head(member_wemedia.getMedia_header());
				art.setPublish_org_name(member_wemedia.getMedia_title());
				// 是否关注
				if (param.containsKey("user_id")) {
					ApiU_follow follow = this.apiU_followMapper.getFollowByCondition(param.get("user_id").toString(),
							art.getOrg_id());
					if (!Util.isNull(follow)) {
						art.setIs_follow(1);
					} else {
						art.setIs_follow(0);
					}
				} else {
					art.setIs_follow(0);
				}
			}
		}
		// apiT_member_wemediaMapper
		return art;
	}

	// public List<U_article> getFavoriteArticles(Map param, PageInfo pageInfo)
	// {
	// return u_articleMapper.getFavoriteArticlesListPage(param, pageInfo);
	// }

	// public List<ApiU_follow> getFollowArticles(Map param, PageInfo pageInfo)
	// {
	//
	// return this.apiU_followMapper.getArticleFollowListPage(param, pageInfo);
	// }

	// 获取推荐的头条文章列表
	public List<U_article> getRecommendArticles(String user_id, PageInfo page) {
		List<U_article> articles = new ArrayList<U_article>();
		Map<String, Object> params = new HashMap<String, Object>();
		if (Util.isNull(user_id)) {
			articles = this.u_articleMapper.getRecommendArticlesByUserIdListPage(null, page);
		} else {
			articles = this.u_articleMapper.getRecommendArticlesByUserIdListPage(user_id, page);
		}
		for (U_article art : articles) {
			params.clear();
			// params.put("user_id", user_id);
			params.put("article_id", art.getId().toString());
			Integer comment_count = this.apiU_article_replyMapper.getCommentCountByMap(params);
			art.setComment_count(comment_count);

			// 图说中图片个数， article_type=2
			if (art.getArticle_type() == 2) {
				List<U_article_resource> resouce_list = this.u_article_resourceMapper
						.getArticleResourcesByArticleID(art.getId().toString());
				art.setImg_count(resouce_list.size());
			}
		}
		return articles;
	}

	// 文章/用户评论数
	public Integer getCommentCountByMap(Map param) {
		return this.apiU_article_replyMapper.getCommentCountByMap(param);
	}

	// 获取收藏的文章列表
	public List<U_article> getArticleFavoriteByUserID(Map param, PageInfo page) {
		List<U_article> article_list = this.u_articleMapper.getFavoriteArticlesListPage(param, page);
		for (U_article art : article_list) {
			// 取评论数
			Map<String, String> params = new HashMap<String, String>();
			params.put("article_id", art.getId().toString());
			Integer commentCount = this.apiU_article_replyMapper.getCommentCountByMap(params);
			art.setComment_count(commentCount);
		}
		return article_list;
	}

	// 取APP首页滚动的最新发的10条头条
	public List<U_article> getHomePageArticleList() {
		return this.u_articleMapper.findNewArticleList();
	}

	public List<U_article> getArticles(String cate_id, String title, String user_id) throws Exception {
		List<U_article> list = new ArrayList<U_article>();
		Map<String, Object> params = new HashMap<String, Object>();
		if (user_id != null) {
			params.put("creater_id", user_id);
		}
		if (title != null && !"".equals(title)) {
			params.put("title", title);
		}
		list = u_articleMapper.findArticlesByMap(params);
		return list;
	}
}
